package com.open.capacity.study.utils;


import cn.hutool.extra.template.TemplateException;
import com.open.capacity.common.model.Stage;
import org.apache.poi.poifs.filesystem.DirectoryEntry;
import org.apache.poi.poifs.filesystem.DocumentEntry;
import org.apache.poi.poifs.filesystem.POIFSFileSystem;

import java.io.ByteArrayInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

public class MyDownLoadUtil {

    public static boolean load(Stage stage) {
        try {
            String name = stage.getStitle()+"验收标准.doc";
            //输出文档路径及名称
            String savePath = "C:\\Backup\\Desktop\\"+name;
            String html = stage.getStcrit();
            html = html.replace("&lt;", "<").replace("&gt;", ">").replace("&quot;", "\"").replace("&amp;", "&");
            String content="<html><body>"+html+"</body></html>";
            byte b[] = content.getBytes("GBK");  //这里是必须要设置编码的，不然导出中文就会乱码。
            ByteArrayInputStream bais = new ByteArrayInputStream(b);//将字节数组包装到流中
            POIFSFileSystem poifs = new POIFSFileSystem();
            DirectoryEntry directory = poifs.getRoot();
            DocumentEntry documentEntry = directory.createDocument("WordDocument", bais);
            OutputStream ostream = new FileOutputStream(savePath);
            poifs.writeFilesystem(ostream);
            bais.close();
            ostream.close();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
        } catch (TemplateException e) {
            e.printStackTrace();
        }
        return false;
    }
}
